﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AgileTimeTracker.Client.Wpf.AccessControlModule;
using AgileTimeTracker.Client.Wpf.Common;
using AgileTimeTracker.Domain.UserModule;

namespace AgileTimeTracker.Client.Wpf
{
    public class MainWindowViewModel : BaseViewModel
    {
        private static MainWindowViewModel instance;

        public static MainWindowViewModel Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new MainWindowViewModel();
                }

                return instance;
            }
        }

        private BaseViewModel currentView;

        private BaseViewModel tempView;

        private User currentUser;

        private bool isTempViewVisible;

        private UserProject currentProject;

        private MainWindowViewModel()
        {
        }

        public BaseViewModel CurrentView
        {
            get { return this.currentView; }
            set 
            {
                if (this.currentView == value)
                {
                    return;
                }
                if (this.currentView != null)
                {
                    this.UnglueBusyInfo(this.currentView);                    
                }                

                this.currentView = value;
                this.GlueBusyInfo(this.currentView);                
                this.OnPropertyChanged("CurrentView");
            }
        }

        public BaseViewModel TempView
        {
            get { return this.tempView; }
            set
            {
                if (this.tempView == value)
                {
                    return;
                }
                if (this.tempView != null)
                {
                    this.UnglueBusyInfo(this.tempView);
                }

                this.tempView = value;
                this.GlueBusyInfo(this.tempView);
                this.OnPropertyChanged("TempView");
            }
        }

        public bool IsTempViewVisible
        {
            get { return this.isTempViewVisible; }
            set { this.OnPropertyChanged(ref this.isTempViewVisible, value); }
        }

        public User CurrentUser
        {
            get { return this.currentUser; }
            set { this.OnPropertyChanged(ref this.currentUser, value); }
        }

        public UserProject CurrentProject
        {
            get { return this.currentProject; }
            set { this.OnPropertyChanged(ref this.currentProject, value); }
        }

        protected override void HandlePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch(e.PropertyName)
            {
                case "CurrentView":
                    HandleCurrentViewChanged();
                    break;
                case "TempView":
                    HandleTempViewChanged();
                    break;
            }
        }

        protected override void Load()
        {
            base.Load();
            this.CurrentView = new LoginViewModel();            
        }

        private void HandleTempViewChanged()
        {
            this.IsTempViewVisible = this.tempView != null;
        }

        private void HandleCurrentViewChanged()
        {
            if(this.currentView == null)
            {
                this.Title = string.Empty;
            }
            else
            {
                this.Title = this.currentView.Title;                
            }
        }        
    }
}
